package models;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

import play.libs.Json;
import play.mvc.WebSocket;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import models.msgs.Info;


public final class Human extends Player {
    private int zetony;
    private WebSocket.In<JsonNode> in;
    private WebSocket.Out<JsonNode> out;
    
    public Human(String _nazwa, int _zetony, WebSocket.In<JsonNode> _in, WebSocket.Out<JsonNode> _out){
	nazwa = _nazwa;
	in = _in;
	out = _out;
    zetony = _zetony;
    }
    
    protected boolean czyGotowy() {
        String text = "";
        final BufferedReader buffRead = 
                new BufferedReader(new InputStreamReader(System.in)); 
        do {
            System.out.print("Czy gracz jest gotowy? [y]");
           
            try { 
                text = buffRead.readLine(); 
            }           
            catch (IOException e) {
                System.out.println("Blad czytania!");
            }
            
            
            
        }
        while(!text.equals("y") && !text.equals("Y")); 
        return true;
    }
    
    public int getZetony(){
        return zetony;
    }
    
    public void setZetony(int zetony){
        this.zetony = zetony;
    }
    
    private void ukryjKarty(){
        for(int j = 0; j < 20; ++j){
            System.out.println();
        }
    }
    
    public Bet makeBet(Handy hand, int pula, int obecnaStawka, boolean czyAllIn, boolean czyBetted){
        czyGotowy();
        System.out.println("Pula wynosi " + Integer.toString(pula));
        System.out.print("Twoje karty to: ");
        hand.show();
        System.out.println();
        System.out.println("Masz " + Integer.toString(zetony) + " zetonow.");
        String text = "";
        Bet bet = null;
        String textStawka = "";
        
        if(!czyAllIn){
            System.out.print("Obecna stawka to " + Integer.toString(obecnaStawka) + ". ");
            do {
                System.out.print("Jaki jest Twoj ruch?");
                
                do{
                    BufferedReader buffRead = new 
                            BufferedReader(new InputStreamReader(System.in));
                    try {
                        text = buffRead.readLine();
                    }
                    catch (IOException e) {
                        System.out.println("Blad czytania!");
                    }
                    if(czyBetted && (text.equals("1") || text.equals("2"))){
                        System.out.print("Ktoś już betowal! Musisz zrobic cos innego: ");
                    }
                }while(czyBetted && (text.equals("1") || text.equals("2")));
            }
            while(!text.equals("1") && !text.equals("2")
                    && !text.equals("3") && !text.equals("6")
                    && !text.equals("4") && !text.equals("5"));
            
            if(text.equals("1")){
                bet = new Bet("check", 0);
            }
            if(text.equals("2")){
                System.out.println("Jaka jest Twoja stawka?");
                BufferedReader buffRead = new 
                        BufferedReader(new InputStreamReader(System.in));
                try {
                    textStawka = buffRead.readLine();
                }
                catch (IOException e) {
                    System.out.println("Blad czytania!");
                }
                int stawkaGracza = Integer.parseInt(textStawka);
                if(zetony >= obecnaStawka + stawkaGracza){
                    bet = new Bet("bet", stawkaGracza);
                }
                else{
                    bet = new Bet("fold", 0 );
                }
            }
            if(text.equals("3")){
                System.out.println("Jaka jest Twoja stawka?");
                BufferedReader buffRead = new 
                        BufferedReader(new InputStreamReader(System.in));
                try {
                    textStawka = buffRead.readLine();
                }
                catch (IOException e) {
                    System.out.println("Blad czytania!");
                }
                int stawkaGracza = Integer.parseInt(textStawka);
                if(zetony > obecnaStawka + stawkaGracza){
                    bet = new Bet("bet", stawkaGracza);
                }
                else{
                    bet = new Bet("fold", 0 );
                }
            }
            if(text.equals("4")){
                bet = new Bet("call", 0);
            }
            if(text.equals("5")){
                bet = new Bet("fold", 0);
            }
            if(text.equals("6")){
                bet = new Bet("all-in", 0);
            }
        }
        else{
            do {
                System.out.println("Zagrano all-in! Czy tez grasz all-in?");
                BufferedReader buffRead = new 
                        BufferedReader(new InputStreamReader(System.in));
                try {
                    text = buffRead.readLine();
                }
                catch (IOException e) {
                    System.out.println("Blad czytania!");
                }
            }
            while(!text.equals("y") && !text.equals("Y")
                    && !text.equals("n") && !text.equals("N"));
            if(text.equals("y") || text.equals("Y")){
                bet = new Bet("all-in", 0);
            }
            else{
                bet = new Bet("fold", 0);
            }
        }
        ukryjKarty();
        return bet;        
    }
    
	public boolean [] wymien(Handy hand) {
	    if(czyGotowy()){
            System.out.print("Twoje karty to: ");
            hand.show();
            System.out.println();
        }
	    int i = 0;
	    String text = "";
	    final int iloscKart = 5;
	    boolean tmp[] = new boolean[iloscKart];
	    do {
	        do {
    	        System.out.print("Czy chcesz wymienic "
    	                + Integer.toString(i + 1) + " karte? [Y/N/0]");
    	        
    	        BufferedReader buffRead = new 
    	                BufferedReader(new InputStreamReader(System.in));
    	        try {
    	            text = buffRead.readLine();
    	        }
    	        catch (IOException e) {
    	            System.out.println("Blad czytania!");
    	        }
	        }
	        while(!text.equals("y") && !text.equals("Y")
	                && !text.equals("n")
	                && !text.equals("N") && !text.equals("0"));
	        
            if (text.equals("y") || text.equals("Y")) {
                tmp[i] = true;
            }
            ++i;
	        
	    }
	    while(i < iloscKart && !text.equals("0"));
	    ukryjKarty();
	    return tmp;
       
	}
	
	public void onReceive(Object msg){
		if(msg instanceof Info)
        {        
        	Info info = (Info) msg;             	
            ObjectNode event = Json.newObject();
            event.put("message", "[ " +/*+ info.getName()+*/ " ] : " + info.getText()); 
            
            out.write(event);
        }
	}
}
